(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("settlementSummaryService", settlementSummaryService);
	settlementSummaryService.$inject = [ "$q", "clsConstants", "$filter", "ajaxServiceFactory", "arithService", "formatService" ];
	function settlementSummaryService($q, clsConstants, $filter, ajaxServiceFactory, arithService, formatService) {
		var translate = $filter("translate");
		function getFxRateTypes() {
			var ajaxService = ajaxServiceFactory.buildAjaxService({
				getFxRateTypes : {
					url : "do/cls/dataList/cdaFxRateTypes",
					method : "get"
				}
			});
			var defer = $q.defer();
			ajaxService.getFxRateTypes().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		}

		function getLabelForOptions(value, options) {
			var optionLabel = value;
			if (options) {
				for (var i = 0; i < options.length; i++) {
					if (value == options[i].value) {
						optionLabel = options[i].label;
						break;
					}
				}
			}
			return optionLabel;
		}

		function initData(settlement) {
			initGeneralTab(settlement);
			var loanInfo = settlement.loan.loanInfo;
			var loanInt = settlement.loan.loanInt;
			if (loanInfo) {
				loanInfo.intSpreadTotalShow = false;
				loanInfo.allInOneRateShow = false;
				if (loanInfo.rpType == "S") {
					if (loanInt.rpIntType == "I" || loanInt.rpIntType == "M" || loanInt.rpIntType == "F") {
						loanInfo.allInOneRateShow = true;
					}
					if (loanInt.rpIntType == "I" || loanInt.rpIntType == "F") {
						loanInfo.intSpreadTotalShow = true;
					}
				} else {
					loanInfo.intSpreadTotalShow = true;
					loanInfo.allInOneRateShow = true;
				}
			}
			settlement.notExistsetlDetailPay = true;
			settlement.notExistsetlDetailReceive = true;
			if (settlement.setlDetails && settlement.setlDetails.length > 0) {
				angular.forEach(settlement.setlDetails, function(data, index, array) {
					if (data.itemSubtype != clsConstants.SetlDetailItemSubType.INTEREST_NO_REFUND) {
						if (data.itemType == "P") {
							data.itemType_text = translate("label.settlement.hinttext.principal");
						} else if (data.itemType == "I") {
							data.itemType_text = translate("label.settlement.hinttext.interest");
						} else if (data.itemType == "F") {
							if (data.itemSubtype == clsConstants.SetlDetailItemSubType.IR_NORMAL_INTEREST || 
									data.itemSubtype == clsConstants.SetlDetailItemSubType.IR_PASTDUE_INTEREST ||
									data.itemSubtype == clsConstants.SetlDetailItemSubType.IR_PASTDUE_PENALTY_INTEREST) {
								data.itemType_text = translate("label.settlement.hinttext.interest");
							} else {
								data.itemType_text = translate("label.settlement.hinttext.fee");
							}
						}
						if (data.itemWaiveFlag == "Y") {
							data.itemType_text += "  (" + translate("label.settlement.hinttext.waived") + ")";
						}
						if (data.itemHoldFlag == "Y") {
							data.itemType_text += "  (" + translate("label.settlement.hinttext.hold") + ")";
						}
						data.taxAmt_text = formatService.formatMoney(data.taxAmt, data.itemCcy);
						if (arithService.gt(data.taxAmt, 0) && data.taxWaiveFlag == "Y") {
							data.taxAmt_text += "  (" + translate("label.settlement.hinttext.waived") + ")";
						}
						if (data.itemCcy != data.payCcy) {
							if (arithService.gt(data.fxRate, 0)) {
								data.fxrate1_text = getLabelForOptions(data.fxRateType, settlement.fxRateTypes) + "  " + data.fxRate;
							}
							if (arithService.gt(data.fxRate2, 0)) {
								data.fxrate2_text = getLabelForOptions(data.fxRateType2, settlement.fxRateTypes) + "  " + data.fxRate2;
							}
						}
						data.desc_text = translate("label.settlement.hinttext.description") + ": ";
						if (data.itemType == "P" || data.itemType == "F") {
							data.desc_text += translate("label.settlement.init." + data.itemType.toLowerCase());
						} else if (data.itemType == "I") {
							var intDate_text = translate("label.settlement.hinttext.from") + ": " + formatService.formatDate(data.intDateFrom, "date") + "--"
									+ translate("label.settlement.hinttext.to") + ": " + formatService.formatDate(data.intDateTo, "date");
							if (data.crDrFlag == "C") {
								if (loanInfo && loanInfo.rpType == "S") {
									data.desc_text += intDate_text;
								} else {
									if (data.itemSubtype == clsConstants.SetlDetailItemSubType.INTEREST_REFUND_HOLD) {
										data.desc_text += translate("label.settlement.hinttext.holdrefund");
									} else {
										data.desc_text += "(" + translate("label.settlement.hinttext.refund") + ")" + intDate_text;
									}
								}
							} else if (data.crDrFlag == "D") {
								var intDate_text_D = translate("label.settlement.hinttext.refund") + ": " + formatService.formatDate(data.intDateFrom, "date") + "--"
										+ translate("label.settlement.hinttext.to") + ": " + formatService.formatDate(data.intDateTo, "date");
								if (loanInfo && loanInfo.rpType == "S") {
									data.desc_text += intDate_text_D;
								} else {
									if (data.itemSubtype == clsConstants.SetlDetailItemSubType.CARRY_OVER_INTEREST) {
										data.desc_text += "(" + translate("label.settlement.hinttext.carryover") + ")";
									}
									data.desc_text += intDate_text;
								}
							}
						}
						data.desc_text += "  (";
						if (data.itemType == "F") {
							data.desc_text += translate("label.settlement.summary.itemcode") + ":" + $filter("encode")(data.itemCode) + "  ";
						}
						data.desc_text += translate("label.settlement.release.itemamt") + ":" + data.itemCcy + " " + formatService.formatMoney(data.itemAmt, data.itemCcy) + ")";
						if (data.fxCrossRateFlag == "X") {
							if (data.fxContractNo) {
								data.desc_text += "  " + translate("label.payment.fxcontractno") + ":" + data.fxContractNo;
							}
							if (!arithService.eq(data.fxCrossRate, 0)) {
								data.desc_text += "  " + translate("label.settlement.init.fxcrossrate") + ":" + data.fxCrossRate;
								data.desc_text += " " + translate("label.settlement.hinttext.from") + ":" + data.itemCcy;
								data.desc_text += " " + translate("label.settlement.hinttext.to") + ":" + data.payCcy;
							}
						}
						if (data.fxCrossRateFlag == "N") {
							if (data.fxContractNo) {
								data.desc_text += "  " + translate("label.payment.fxcontractno") + ":" + data.fxContractNo;
							}
						}
						if (data.crDrFlag == "C") {
							settlement.notExistsetlDetailPay = false;
						} else if (data.crDrFlag == "D") {
							settlement.notExistsetlDetailReceive = false;
						}
					}
				});
			}
		}

		function initGeneralTab(settlement) {
			settlement.generalHeading = translate("title.tab.settlement.loansummary");
			if (settlement.settlementInfo && settlement.settlementInfo.parentType == "CUSTPAY") {
				settlement.generalHeading = translate("title.tab.settlement.custpay");
			} else if (settlement.settlementInfo && settlement.settlementInfo.parentType == "DCHG") {
				settlement.generalHeading = translate("title.tab.settlement.directcharge");
			}
		}

		function isDetailsPay(item) {
			if (item.crDrFlag == "C" && item.itemSubtype != clsConstants.SetlDetailItemSubType.INTEREST_NO_REFUND) {
				return true;
			}
			return false;
		}

		function isDetailsReceive(item) {
			if (item.crDrFlag == "D" && item.itemSubtype != clsConstants.SetlDetailItemSubType.INTEREST_NO_REFUND) {
				return true;
			}
			return false;
		}

		function isRollover(settlement) {
			if (settlement.loan && settlement.loan.loanInfo && settlement.loan.loanInfo.rolloverFlag && settlement.loan.loanInfo.rolloverFlag == "Y"
				&& settlement.settlementInfo && settlement.settlementInfo.parentType == "ISSUE") {
				return true;
			} else {
				return false;
			}
		}

		function processLoanPaidLoans(loanPaidLoans) {
			if (loanPaidLoans && loanPaidLoans.length > 0) {
				angular.forEach(loanPaidLoans, function(paidLoan) {
					paidLoan.paidIntAmt_abs = Math.abs(paidLoan.paidIntAmt);
					paidLoan.paidFeeAmt_abs = Math.abs(paidLoan.paidFeeAmt);
					paidLoan.intAmt_abs = Math.abs(paidLoan.intAmt);
					paidLoan.feeAmt_abs = Math.abs(paidLoan.feeAmt);
				});
			}
		}
		
		function processLoanPaidCustpays(loanPaidCustpays) {
			if (loanPaidCustpays && loanPaidCustpays.length > 0) {
				angular.forEach(loanPaidCustpays, function(paidCustpay) {
					paidCustpay.paidFeeAmt_abs = Math.abs(paidCustpay.paidFeeAmt);
					paidCustpay.feeAmt_abs = Math.abs(paidCustpay.feeAmt);
				});
			}
		}

		return {
			getFxRateTypes : getFxRateTypes,
			initData : initData,
			isDetailsPay : isDetailsPay,
			isDetailsReceive : isDetailsReceive,
			isRollover : isRollover,
			processLoanPaidLoans : processLoanPaidLoans,
			processLoanPaidCustpays : processLoanPaidCustpays
		};
	}
})(angular);
